import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';

import 'package:mr_camera/model/UserDetail.dart';
import 'package:mr_camera/mr_camera.dart';
import 'package:mr_camera_example/util/PermissionHelper.dart';
import 'package:logger/logger.dart';

var logger = Logger();

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _mrCameraPlugin = MrCamera();
  UserDetail userDetail = UserDetail();

  static const MethodChannel _channel = MethodChannel('com.acraftsman/mr_camera');

  //抄表杆数量
  int _ipCamCount = 0;

  //拍照结果
  String _takePhotoResult = "unKnow";

  //服务状态
  bool _isScanServiceRunning = false;


  @override
  void initState() {
    super.initState();
    requestPer();
    _mrCameraPlugin.startScanService();
    newUserDetail();
    _channel.setMethodCallHandler(_setupMethodChannel);
  }

  Future<void> requestPer() async{
    await PermissionHelper.requestAllPermission().then((value) => {
      if(value){
        popApp()
      }
    });
  }

  Future<void> popApp() async {
    await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  }

  //接收原生methodChannel
  Future<void> _setupMethodChannel(MethodCall call) async {
    if (call.method == 'sendPhotoResult') {
      String takePhotoResult = call.arguments;
      setState(() {
        _takePhotoResult = takePhotoResult;
      });
    }
  }

  void newUserDetail(){
    userDetail.index = '1';
    userDetail.username = 'usernameddddd';
    userDetail.userNo = '5894';
    userDetail.phone = '17670343098';
    userDetail.address = '湖南省';
    userDetail.startNum = '23456';
  }

  void setIpCamCount() async{
    int ipCamCount = await _mrCameraPlugin.getIpCamCount();
    setState(() {
      _ipCamCount = ipCamCount;
    });
  }

  void setIsScanServiceRunning() async{
    bool isScanServiceRunning = await _mrCameraPlugin.isScanServiceRunning();
    setState(() {
      isScanServiceRunning = isScanServiceRunning;
    });
    print("The isScanServiceRunning is: $isScanServiceRunning");
  }

  void getTakeResult() async {
    String takeResult = await _mrCameraPlugin.startClientActivity(userDetail,'/storage/emulated/0/mrCamera/20240624094600001.jpg');
    setState(() {
      _takePhotoResult = takeResult;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Padding(
            padding: const EdgeInsets.all(32),
            child: Column(
                children:[
                  ElevatedButton(
                    onPressed: () async {
                      getTakeResult();
                    },
                    child: Text('跳转到抄表杆抄表页面'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      _mrCameraPlugin.stopScanService();
                    },
                    child: Text('停止抄表杆服务'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      _mrCameraPlugin.startScanService();
                    },
                    child: Text('启动抄表杆服务'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      setIpCamCount();
                    },
                    child: Text('刷新抄表杆数量----${_ipCamCount}'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      setIsScanServiceRunning();
                    },
                    child: Text('获取抄表杆服务状态'),
                  ),
                  Text('拍照结果----${_takePhotoResult}'),
                  Text('scanService状态----${_isScanServiceRunning}')
                ]
            ),
          ),
        ),
      ),
    );
  }
}
